package com.ysd.lis.service.eq.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysd.common.Result;
import com.ysd.lis.entity.eq.EqSpotcheckTask;
import com.ysd.lis.entity.eq.EqSpotcheckTaskEq;
import com.ysd.lis.entity.eq.EqSpotcheckTaskEqSt;
import com.ysd.lis.entity.eq.EqSpotcheckTaskEqStIt;
import com.ysd.lis.mapper.eq.EqSpotcheckTaskEqMapper;
import com.ysd.lis.mapper.eq.EqSpotcheckTaskEqStItMapper;
import com.ysd.lis.mapper.eq.EqSpotcheckTaskEqStMapper;
import com.ysd.lis.mapper.eq.EqSpotcheckTaskMapper;
import com.ysd.lis.request.CommonPageDto;
import com.ysd.lis.service.SysRuleSetService;
import com.ysd.lis.service.eq.EqSpotcheckTaskEqStItService;
import com.ysd.lis.service.eq.EqSpotcheckTaskEqStService;
import com.ysd.lis.service.eq.EqSpotcheckTaskService;
import com.ysd.util.ToolsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 设备点检记录 服务实现类
 * </p>
 *
 * @author WH
 * @since 2024-08-26
 */
@Service
public class EqSpotcheckTaskServiceImpl extends ServiceImpl<EqSpotcheckTaskMapper, EqSpotcheckTask> implements EqSpotcheckTaskService {

    @Autowired
    private EqSpotcheckTaskMapper eqSpotcheckTaskMapper;
    @Autowired
    private EqSpotcheckTaskEqMapper eqSpotcheckTaskEqMapper;
    @Autowired
    private EqSpotcheckTaskEqStMapper eqSpotcheckTaskEqStMapper;
    @Autowired
    private EqSpotcheckTaskEqStService eqSpotcheckTaskEqStService;
    @Autowired
    private EqSpotcheckTaskEqStItMapper eqSpotcheckTaskEqStItMapper;
    @Autowired
    private EqSpotcheckTaskEqStItService eqSpotcheckTaskEqStItService;

    @Autowired
    public SysRuleSetService sysRuleSetService;

    @Override
    public Result findSpotcheckTaskPageList(CommonPageDto pageParam) {
        Page<EqSpotcheckTask> page = new Page<>();
        page.setCurrent(pageParam.getPageIndex());
        page.setSize(pageParam.getPageSize());
        LambdaQueryWrapper<EqSpotcheckTask> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(EqSpotcheckTask::getDelFlag, 0);
        lambdaQueryWrapper.eq(StringUtils.isNotBlank(pageParam.getStatus()), EqSpotcheckTask::getStatus, pageParam.getStatus());
        if (StringUtils.isNotBlank(pageParam.getSeachValue())) {
            lambdaQueryWrapper.and(wrapper -> wrapper.like(EqSpotcheckTask::getCode, pageParam.getSeachValue())
                    .or().like(EqSpotcheckTask::getName, pageParam.getSeachValue()));
        }
        lambdaQueryWrapper.orderByDesc(EqSpotcheckTask::getCreateTime);
        Page<EqSpotcheckTask> orderPage = eqSpotcheckTaskMapper.selectPage(page, lambdaQueryWrapper);
        List<EqSpotcheckTask> records = orderPage.getRecords();
        for (EqSpotcheckTask order : records) {
            String maintainStatus = order.getStatus();
            if (!"10".equals(maintainStatus)) {
                Timestamp nextDate = order.getPlanDate();
                if (ToolsUtils.isNotEmpty(nextDate)) {
                    long diff = Math.abs(new Date().getTime() - nextDate.getTime());
                    order.setOverdue(diff / (1000 * 60 * 60 * 24));
                }

            }
        }
        return Result.succ(1, "查询成功", orderPage);
    }

    @Override
    public Result addSpotcheckTask(EqSpotcheckTask eqSpotcheckTask) {
        //添加设备巡检任务
        String a = sysRuleSetService.createRuleCodeList("EqSpotcheckTaskCode", 1).get(0);
        eqSpotcheckTask.setCode(a);
        eqSpotcheckTaskMapper.insert(eqSpotcheckTask);
        List<EqSpotcheckTaskEq> spotcheckTaskEqs = eqSpotcheckTask.getSpotcheckTaskEqList();
        if (CollectionUtils.isNotEmpty(spotcheckTaskEqs)) {
            for (EqSpotcheckTaskEq eqSpotcheckTaskEq : spotcheckTaskEqs) {
                eqSpotcheckTaskEq.setTaskId(eqSpotcheckTask.getId());
                //添加设备巡检任务设备
                eqSpotcheckTaskEqMapper.insert(eqSpotcheckTaskEq);
                List<EqSpotcheckTaskEqSt> patrolTaskEqStList = eqSpotcheckTaskEq.getSpotcheckTaskEqStList();
                List<EqSpotcheckTaskEqStIt> patrolTaskEqStItList = eqSpotcheckTaskEq.getSpotcheckTaskEqStItList();

                String isNormal = "";
                List<String> noPass = new ArrayList<>();
                List<String> noCheck = new ArrayList<>();
                List<String> isNormales = patrolTaskEqStItList.stream().filter(item -> StringUtils.isNotBlank(item.getIsNormal())).map(EqSpotcheckTaskEqStIt::getIsNormal).distinct().collect(Collectors.toList());

                if (CollectionUtils.isNotEmpty(isNormales)) {
                    isNormal = "正常";
                    for (String item : isNormales) {
                        if ("异常".equals(item)) {
                            isNormal = item;
                            break;
                        }
                    }
                }
                for (EqSpotcheckTaskEqStIt eqPatrolTaskEqStIt : patrolTaskEqStItList) {
                    if ("异常".equals(eqPatrolTaskEqStIt.getIsNormal())) {
                        noPass.add(eqPatrolTaskEqStIt.getStandardName());
                    }
                }
                for (EqSpotcheckTaskEqStIt eqPatrolTaskEqStIt : patrolTaskEqStItList) {
                    if (StringUtils.isBlank(eqPatrolTaskEqStIt.getCheckResult())) {
                        noCheck.add(eqPatrolTaskEqStIt.getStandardName());
                    }
                }
                List<String> noPassEs = noPass.stream().distinct().collect(Collectors.toList());
                eqSpotcheckTaskEq.setCheckResult(isNormal);
                eqSpotcheckTaskEq.setNoPass(String.join(",", noPassEs));
                eqSpotcheckTaskEq.setNoCheck(String.join(",", noCheck));
                eqSpotcheckTaskEqMapper.updateById(eqSpotcheckTaskEq);

                //添加巡检任务设备的巡检标准
                for (EqSpotcheckTaskEqSt eqPatrolTaskEqSt : patrolTaskEqStList) {
                    eqPatrolTaskEqSt.setTaskId(eqSpotcheckTask.getId());
                    eqPatrolTaskEqSt.setTaskEqId(eqSpotcheckTaskEq.getId());
                    eqSpotcheckTaskEqStMapper.insert(eqPatrolTaskEqSt);
                    List<EqSpotcheckTaskEqStIt> eqPatrolTaskEqStIts = patrolTaskEqStItList.stream().filter(eqPatrolTaskEqStIt -> eqPatrolTaskEqStIt.getStandardId().equals(eqPatrolTaskEqSt.getStandardId())).collect(Collectors.toList());
                    //添加巡检任务设备的巡检标准项
                    if (CollectionUtils.isNotEmpty(eqPatrolTaskEqStIts)) {
                        for (EqSpotcheckTaskEqStIt eqPatrolTaskEqStIt : eqPatrolTaskEqStIts) {
                            eqPatrolTaskEqStIt.setTaskId(eqSpotcheckTask.getId());
                            eqPatrolTaskEqStIt.setTaskEqId(eqSpotcheckTaskEq.getId());
                            eqPatrolTaskEqStIt.setTaskEqStId(eqPatrolTaskEqSt.getId());
                        }
                        eqSpotcheckTaskEqStItService.saveBatch(eqPatrolTaskEqStIts);
                    }
                }
            }
        }
        return Result.succ(1, "添加成功", eqSpotcheckTask);
    }

    @Override
    public Result editSpotcheckTask(EqSpotcheckTask eqSpotcheckTask) {
        eqSpotcheckTaskMapper.updateById(eqSpotcheckTask);
        List<EqSpotcheckTaskEq> patrolTaskEqList = eqSpotcheckTask.getSpotcheckTaskEqList();
        if (CollectionUtils.isNotEmpty(patrolTaskEqList)) {
            for (EqSpotcheckTaskEq eqPatrolTaskEq : patrolTaskEqList) {
                List<EqSpotcheckTaskEqSt> patrolTaskEqStList = eqPatrolTaskEq.getSpotcheckTaskEqStList();
                List<EqSpotcheckTaskEqStIt> patrolTaskEqStItList = eqPatrolTaskEq.getSpotcheckTaskEqStItList();

                String isNormal = "";
                List<String> noPass = new ArrayList<>();
                List<String> noCheck = new ArrayList<>();
                List<String> isNormales = patrolTaskEqStItList.stream().filter(item -> StringUtils.isNotBlank(item.getIsNormal())).map(EqSpotcheckTaskEqStIt::getIsNormal).distinct().collect(Collectors.toList());

                if (CollectionUtils.isNotEmpty(isNormales)) {
                    isNormal = "正常";
                    for (String item : isNormales) {
                        if ("异常".equals(item)) {
                            isNormal = item;
                            break;
                        }
                    }
                }
                for (EqSpotcheckTaskEqStIt eqPatrolTaskEqStIt : patrolTaskEqStItList) {
                    if ("异常".equals(eqPatrolTaskEqStIt.getIsNormal())) {
                        noPass.add(eqPatrolTaskEqStIt.getStandardName());
                    }
                }
                for (EqSpotcheckTaskEqStIt eqPatrolTaskEqStIt : patrolTaskEqStItList) {
                    if (StringUtils.isBlank(eqPatrolTaskEqStIt.getCheckResult())) {
                        noCheck.add(eqPatrolTaskEqStIt.getStandardName());
                    }
                }
                List<String> noPassEs = noPass.stream().distinct().collect(Collectors.toList());
                eqPatrolTaskEq.setCheckResult(isNormal);
                eqPatrolTaskEq.setNoPass(String.join(",", noPassEs));
                eqPatrolTaskEq.setNoCheck(String.join(",", noCheck));
                eqSpotcheckTaskEqMapper.updateById(eqPatrolTaskEq);


                if (CollectionUtils.isNotEmpty(patrolTaskEqStList)) {
                    eqSpotcheckTaskEqStService.saveOrUpdateBatch(patrolTaskEqStList);
                }
                if (CollectionUtils.isNotEmpty(patrolTaskEqStItList)) {
                    eqSpotcheckTaskEqStItService.saveOrUpdateBatch(patrolTaskEqStItList);
                }
            }
        }
        return Result.succ(1, "编辑成功", eqSpotcheckTask);
    }

    @Override
    public Result findSpotcheckTaskById(EqSpotcheckTask eqSpotcheckTask) {
        EqSpotcheckTask eqSpotcheckTask1 = eqSpotcheckTaskMapper.selectById(eqSpotcheckTask.getId());
        List<EqSpotcheckTaskEq> eqSpotcheckTaskEqs = eqSpotcheckTaskEqMapper.selectList(new LambdaQueryWrapper<EqSpotcheckTaskEq>().eq(EqSpotcheckTaskEq::getTaskId, eqSpotcheckTask1.getId()).eq(EqSpotcheckTaskEq::getDelFlag, 0));
        for (EqSpotcheckTaskEq eqSpotcheckTaskEq : eqSpotcheckTaskEqs) {
            List<EqSpotcheckTaskEqSt> eqPatrolTaskEqSts = eqSpotcheckTaskEqStMapper.selectList(new LambdaQueryWrapper<EqSpotcheckTaskEqSt>().eq(EqSpotcheckTaskEqSt::getTaskEqId, eqSpotcheckTaskEq.getId()).eq(EqSpotcheckTaskEqSt::getDelFlag, 0));
            List<EqSpotcheckTaskEqStIt> eqPatrolTaskEqStIts = eqSpotcheckTaskEqStItMapper.selectList(new LambdaQueryWrapper<EqSpotcheckTaskEqStIt>().eq(EqSpotcheckTaskEqStIt::getTaskEqId, eqSpotcheckTaskEq.getId()).eq(EqSpotcheckTaskEqStIt::getDelFlag, 0));
            eqSpotcheckTaskEq.setSpotcheckTaskEqStList(eqPatrolTaskEqSts);
            eqSpotcheckTaskEq.setSpotcheckTaskEqStItList(eqPatrolTaskEqStIts);
        }
        eqSpotcheckTask1.setSpotcheckTaskEqList(eqSpotcheckTaskEqs);
        return Result.succ(1, "查询成功!", eqSpotcheckTask1);
    }

    @Override
    public Result findSpotcheckTaskByPlanId(EqSpotcheckTask eqSpotcheckTask) {
        LambdaQueryWrapper<EqSpotcheckTask> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EqSpotcheckTask::getPlanId, eqSpotcheckTask.getPlanId());
        queryWrapper.eq(EqSpotcheckTask::getDelFlag, 0);
        List<EqSpotcheckTask> eqPatrolTasks = eqSpotcheckTaskMapper.selectList(queryWrapper);
        return Result.succ(1, "查询成功!", eqPatrolTasks);
    }

    @Override
    public Result deleteSpotcheckTask(List<EqSpotcheckTask> eqSpotcheckTasks) {
        List<String> ids = eqSpotcheckTasks.stream().map(EqSpotcheckTask::getId).collect(Collectors.toList());
        eqSpotcheckTaskMapper.deleteBatchIds(ids);
        eqSpotcheckTaskEqMapper.delete(new LambdaQueryWrapper<EqSpotcheckTaskEq>().in(EqSpotcheckTaskEq::getTaskId, ids).eq(EqSpotcheckTaskEq::getDelFlag, 0));
        eqSpotcheckTaskEqStMapper.delete(new LambdaQueryWrapper<EqSpotcheckTaskEqSt>().in(EqSpotcheckTaskEqSt::getTaskId, ids).eq(EqSpotcheckTaskEqSt::getDelFlag, 0));
        eqSpotcheckTaskEqStItMapper.delete(new LambdaQueryWrapper<EqSpotcheckTaskEqStIt>().in(EqSpotcheckTaskEqStIt::getTaskId, ids).eq(EqSpotcheckTaskEqStIt::getDelFlag, 0));
        return Result.succ(1, "删除点检任务成功", "");
    }
}
